<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/7 0007
 * Time: 16:27
 */

namespace Admin\Controller;

use Think\Page;
use Think\Upload;

class OtheraddController extends AdminController
{

    public function add()
    {
        $this->display();
    }

    public function recruit()
    {
        $p = I('p', '1');
        $page_size = 20;
        $recruit = M('Recruit');
        $count = $recruit->count();
        $list = $recruit->order('update_time desc')->page($p . ',' . $page_size)->select();
        $page = new Page($count, $page_size);
        $this->assign('page', $page->show());
        $this->assign('list', $list);
        $this->display();
    }

    public function edit()
    {
        $id = I('id', '');
        $recruit = M('Recruit')->where(array('id' => $id))->find();
        if ($recruit) {
            $this->assign('recruit', $recruit);
            $this->display();
        } else {
            $this->error('查询失败!');
        }
    }

    public function update()
    {
        $id = I('id', '');
        $data['title'] = I('title', '');
        $data['show'] = I('show', 0);
        $data['comname'] = I('comname', '');
        $data['telphone'] = I('telphone', '');
        $data['address'] = I('address', '');
        $data['update_time'] = date('Y-m-d H:i:s');
        $data['message'] = I('message', '');
        $recruit = M('Recruit');
        $count = $recruit->where('id=' . $id)->count();
        if ($count) {
            $res = $recruit->where('id=' . $id)->data($data)->save();
            if ($res) {
                $this->success('修改成功!', U('recruit'));
            } else {
                $this->error('修改失败!', U('recruit'));
            }
        } else {
            $data['create_time'] = date('Y-m-d H:i:s');
            $res = $recruit->data($data)->add();
            if ($res) {
                $this->success('添加成功!', U('recruit'));
            } else {
                $this->error('添加失败!', U('recruit'));
            }
        }
    }

    public function delete()
    {
        $id = I('id', 0);
        if ($id) {
            $res = M('Recruit')->where('id=' . $id)->delete();
            if ($res) {
                $this->success("删除成功!");
            } else {
                $this->error("删除失败！");
            }
        } else {
            $this->error("删除失败!");
        }
    }

    public function mark()
    {
        $recruit = M('Recruit');
        $id = I('id', 0);
        $show = I('show', 0);
        if ($show == 0 || $show == 1) {
            $data['show'] = $show;
            $data = $recruit->where("id=" . $id)->data($data)->save();
            if ($data) {
                $this->success("修改成功!");
            } else {
                $this->error("修改失败!");
            }
        } else {
            $this->error("修改失败!");
        }
    }

    public function about()
    {
        $p = I('p', '1');
        $page_size = 20;
        $about_index = M('About');
        $count = $about_index->count();
        $about = $about_index->order('post_time desc')->page($p . ',' . $page_size)->select();
        $page = new Page($count, $page_size);
        $this->assign('page', $page->show());
        $this->assign('about', $about);
        $this->display();
    }

    public function about_update()
    {
        $id = I('id', '1');
        $data['content'] = I('content', '');
        $about = M('About');
        $count = $about->where('id=' . $id)->count();
        if ($count) {
            $res = $about->where('id=' . $id)->data($data)->save();
            if ($res) {
                $this->success('修改成功!', U('about_edit?id=1'));
            } else {
                $this->error('修改失败!', U('about_edit?id=1'));
            }
        } else {
            $data['id'] = 1;
            $data['post_time'] = date('Y-m-d H:i:s');
            $res = $about->data($data)->add();
            if ($res) {
                $this->success('添加成功!', U('about_edit?id=1'));
            } else {
                $this->error('添加失败!', U('about_edit?id=1'));
            }
        }

    }

    public function about_edit()
    {
        $id = I('id', '');
        $about = M('About')->where(array('id' => $id))->find();
        if ($about) {
            $this->assign('about', $about);
            $this->display();
        } else {
            $this->display();
        }
    }

    public function help()
    {
        $p = I('p', '1');
        $page_size = 20;
        $help_in = M('Help');
        $count = $help_in->count();
        $help = $help_in->order('post_time desc')->page($p . ',' . $page_size)->select();
        $page = new Page($count, $page_size);
        $this->assign('page', $page->show());
        $this->assign('help', $help);
        $this->display();
    }

    public function help_update()
    {
        $id = I('id', '1');
        $data['content'] = I('content', '');
        $help = M('Help');
        $count = $help->where('id=' . $id)->count();
        if ($count) {
            $res = $help->where('id=' . $id)->data($data)->save();
            if ($res) {
                $this->success('修改成功!', U('help_edit?id=1'));
            } else {
                $this->error('修改失败!', U('help_edit?id=1'));
            }
        } else {
            $data['id'] = 1;
            $res = $help->data($data)->add();
            if ($res) {
                $this->success('添加成功!', U('help_edit?id=1'));
            } else {
                $this->error('添加失败!', U('help_edit?id=1'));
            }
        }

    }

    public function help_edit()
    {
        $id = I('id', '');
        $help = M('Help')->where(array('id' => $id))->find();
        if ($help) {
            $this->assign('help', $help);
            $this->display();
        } else {
            $this->display();
        }
    }

    public function weibo()
    {
        $weibo = M('Url');
        $weibo = $weibo->find();
        $this->assign('weibo', $weibo);
        $this->display();
    }

    public function weibo_update()
    {
        $id = I('id', '1');
        $data['url'] = I('url', '');
        $data['short'] = "微博";
        $data['status'] = 1;
        $weibo = M('Url');
        $count = $weibo->where('id=' . $id)->count();
        if ($count) {
            $res = $weibo->where('id=' . $id)->data($data)->save();
            if ($res) {
                $this->success('修改成功!', U('weibo'));
            } else {
                $this->error('修改失败!', U('weibo'));
            }
        } else {
            $data['create_time'] = time();
            $res = $weibo->data($data)->add();
            if ($res) {
                $this->success('添加成功!', U('weibo'));
            } else {
                $this->error('添加失败!', U('weibo'));
            }
        }

    }

    public function weibo_edit()
    {
        $id = I('id', '');
        $weibo = M('Url')->where(array('id' => $id))->find();
        if ($weibo) {
            $this->assign('weibo', $weibo);
            $this->display();
        } else {
            $this->display();
        }
    }

    public function wechat()
        {
            $wechat = M('Wechat');
            $wechat = $wechat->find();
            $this->assign('wechat', $wechat);
            $this->display();
        }



     public function wechat_update()
         {
             $id = I('id', '');
             $url = I('url', '');
             //$create_time = date('Y-m-d H:i:s');

             $data = array();
             //$data['create_time'] = $create_time;
             $data['url'] = $url;

             $upload = new Upload();// 实例化上传类
             $upload->maxSize = 3145728;// 设置附件上传大小
             $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
             $upload->savePath = '/Picture/Admin/'; // 设置附件上传目录
             $file = $upload->upload();
             if ($file) {
                 $url_t = $file['image']['savepath'] . $file['image']['savename'];
                 $data['url'] = $url_t;
             } else if ($url == '') {
                 $this->error($upload->getError());
             }
             $wechat = M('Wechat');

             if (empty($id)) {
                 $res = $wechat->data($data)->add();
                 if ($res) {
                     $this->success("添加成功", U('wechat'));
                 } else {
                     $this->error("添加失败", U('wechat'));
                 }
             } else {

                 $res = $wechat->where(array('id' => $id))->data($data)->save();
                 if ($res) {
                     $this->success("修改成功", U('wechat'));
                 } else {
                     $this->error("修改失败", U('wechat'));
                 }
             }
         }


        public function wechat_edit()
            {
                $id = I('id', '');
                $wechat = M('Wechat')->where(array('id' => $id))->find();
                if ($wechat) {
                    $this->assign('wechat', $wechat);
                    $this->display();
                } else {
                    $this->display();
                }
            }
}